/*   
 * Copyright (c) 2010-2020 Founder Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * Founder. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Founder.   
 *   
 */
package com.wujw.auto.utils;

import java.io.FileReader;
import java.io.IOException;
import java.util.*;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.lang3.StringUtils;

/**
 * 读取配置信息
 * 
 * @author wujw
 * @version 1.0, 2017年11月11日
 */
public class ConfigUtil {
    /** 项目的路径 */
    public static String projectPath;
    /** 是否生成bean实体 合实体包名 */
    public static boolean beanFlag;
    public static String beanPackage;
    /** 生成Dao接口的参数 */
    public static boolean daoFlag;
    public static String daoPackage;
    /** 生成Service接口的参数 */
    public static boolean serviceFlag;
    public static String servicePackage;
    /** 生成ServiceImpl实现类的参数 */
    public static boolean serviceImplFlag;
    public static String serviceImplPackage;
    /** 生成Mapper.xml的参数 */
    public static boolean mapperXmlFlag;
    public static String mapperXmlPackage;
    /** 需要生成的表 */
    public static Set<String> tablesList;

    // 加载配置文件
    static {
        try {
            /*InputStream ins = ConfigUtil.class.getClassLoader().getResourceAsStream("config.properties");
            Properties pro = new Properties();
            pro.load(ins);

            // 得到配置信息
            projectPath = pro.getProperty("projectPath");
            beanFlag = pro.getProperty("beanFlag");
            beanPackage = pro.getProperty("beanPackage");
            daoFlag = pro.getProperty("daoFlag");
            daoPackage = pro.getProperty("daoPackage");
            serviceFlag = pro.getProperty("serviceFlag");
            servicePackage = pro.getProperty("servicePackage");
            mapperXmlFlag = pro.getProperty("mapperXmlFlag");
            mapperXmlPackage = pro.getProperty("mapperXmlPackage");
            serviceImplFlag = pro.getProperty("serviceImplFlag");
            serviceImplPackage = pro.getProperty("serviceImplPackage");*/
            
            JsonParser parser=new JsonParser();  //创建JSON解析器
            String pathname=ConfigUtil.class.getClassLoader().getResource("config.json").getPath();
            JsonObject object=(JsonObject) parser.parse(new FileReader(pathname));  //创建JsonObject对象
            
            projectPath = object.get("projectPath").getAsString();
            beanFlag = object.get("beanFlag").getAsBoolean();
            beanPackage = object.get("beanPackage").getAsString();
            mapperXmlFlag = object.get("mapperXmlFlag").getAsBoolean();
            mapperXmlPackage = object.get("mapperXmlPackage").getAsString();
            //读取需要生成的表信息
            String tables = object.get("tables").getAsString();

            tablesList = new HashSet<>();

            if (StringUtils.isNotEmpty(tables)) {
                tablesList.addAll(Arrays.asList(tables.split(",")));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
